package com.azkj.album.presenter;

import com.azkj.album.dto.UploadBean;
import com.azkj.album.network.NetworkMaster;
import com.azkj.album.network.callback.UploadCallback;
import com.azkj.album.network.networkframe.bean.BaseResp;
import com.azkj.album.network.networkframe.net.exception.ApiException;
import com.azkj.album.view.base.BasePresenter;
import com.azkj.album.view.iview.IUploadView;
import com.azkj.album.view.widgets.dialog.DialogHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class UpLoadPresenter extends BasePresenter {

    private final IUploadView iView;

    private int i = 1;
    private ArrayList<String> resultList;

    public UpLoadPresenter(IUploadView iView) {
        this.iView = iView;
    }


    public void uploadFile(List<String> paths) {

//        long size = file.length();
//        float mb = (float) (size / 1024 / 1024);
//        if (mb > 3) {
//            ToastUtils.showCenterToast("logo不可以超过3m，请您压缩裁剪图片。");
//            return;
//        }

        i = 1;
        resultList = new ArrayList<>();
        DialogHelper.showLoadingDialog("上传中", 0);

        for (String path : paths) {

            File file = new File(path);

            NetworkMaster.getInstance().getCommonService().uploadFile(file, new UploadCallback<BaseResp<UploadBean>>() {
                @Override
                public void onSuccess(BaseResp<UploadBean> jsonObj) {

                    if (jsonObj.getCode() == 1) {
                        i++;
                        resultList.add(jsonObj.getData().getFullurl());
                        if (i==paths.size()){
                            DialogHelper.hideDialog();
                            iView.uploadSuccess(resultList);
                        }




                    } else {
                        DialogHelper.hideDialog();
                        iView.uploadFail(jsonObj.getMsg());
                    }

                }


                @Override
                public void onError(ApiException apiException) {
                    DialogHelper.hideDialog();
                    iView.uploadFail(apiException.errorInfo.error);

                }

                @Override
                public void onProgress(long currentLength, long contentLength) {

                }
            });


        }


    }

    public void uploadPath(String path) {

        NetworkMaster.getInstance().getCommonService().uploadPath(path, new UploadCallback<BaseResp<UploadBean>>() {
            @Override
            public void onSuccess(BaseResp<UploadBean> jsonObj) {

                if (jsonObj.getCode() == 1) {

                    iView.uploadPathSuccess(jsonObj.getData());
                } else {
                    DialogHelper.hideDialog();
                    iView.uploadFail(jsonObj.getMsg());
                }

            }


            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iView.uploadFail(apiException.errorInfo.error);

            }

            @Override
            public void onProgress(long currentLength, long contentLength) {

            }
        });


    }

}
